package com.wm.web.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Collect;
import com.wm.bean.ResMenu;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.CollectService;
import com.wm.service.ResMenuService;
import com.wm.service.RestaurantService;
import com.wm.service.UserService;
import com.wm.web.servlet.AdminSession;
import com.wm.web.servlet.UserSession;

public class CollectAction extends BaseAction implements SessionAware {
	private static final Log logger = LogFactory.getLog(CollectAction.class);
	
	private CollectService collectService;
	private ResMenuService resMenuService;
	private RestaurantService restaurantService;

	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}

	public ResMenuService getResMenuService() {
		return resMenuService;
	}

	public void setResMenuService(ResMenuService resMenuService) {
		this.resMenuService = resMenuService;
	}

	public CollectService getCollectService() {
		return collectService;
	}

	public void setCollectService(CollectService collectService) {
		this.collectService = collectService;
	}
	
	private Map session;

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}
	
	private long id;					//主键ID
	private String ids;
	private long objId;					//对象ID
	private int objType;				//类型ID
	private List list;
	private List collectList;
	private int listSize;
	private String result;

	public List getCollectList() {
		return collectList;
	}

	public void setCollectList(List collectList) {
		this.collectList = collectList;
	}
	
	public int getListSize() {
		return listSize;
	}

	public void setListSize(int listSize) {
		this.listSize = listSize;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public long getObjId() {
		return objId;
	}

	public void setObjId(long objId) {
		this.objId = objId;
	}

	public int getObjType() {
		return objType;
	}

	public void setObjType(int objType) {
		this.objType = objType;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	public String execute() {
		try {
			if(this.getSession().get(Constant.USER_SESSION_KEY)!=null){
				return this.executeMethod(this.getAction());
			}
			return "default";
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		this.setList(this.getCollectService().findCollectByUserIdObjType(us.getUid(),this.getObjType()));
		this.setListSize(this.getList().size());
		this.setAction("index");
		return "index";
	}
	
	/**
	 * 收藏店铺
	 * @return
	 */
	public String saveRes(){
		if(this.getObjId()>0){
			UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
//			ResMenu r = this.getResMenuService().findResMenuById(this.getId());
//			if(r==null){
//				this.setResult("2");
//				return "collectAjax";
//			}
			//result 1 已存在 0 成功 2 失败
			
			Collect c = this.getCollectService().findCollectByUserIdObjIdObjType(us.getUid(), this.getObjId(), this.getObjType());
			if(c!=null){
				this.setResult("1");
				return "collectAjax";
			}
			c = new Collect();
			c.setUserId(us.getUid());
			c.setObjId(this.getObjId());
			c.setObjType(this.getObjType());
			c.setAddTime(new Date());
			c = this.getCollectService().saveCollect(c);
			if(c.getId()>0){
				this.setResult("0");
			}else{
				this.setResult("2");
			}
		}
		return "collectAjax";
	}
	
	/**
	 * 列表页通过ajax更新收藏列表
	 * @return
	 */
	public String resAjax(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		//this.setList(this.getCollectService().findCollectByUserIdObjTypeFirstResultMaxResults(us.getUid(),0, "id", 1, 0, 5));
		this.setList(this.getCollectService().findCollectByUserIdObjType(us.getUid(),this.getObjType()));
		return "collectAjax2";
	}
	
	/**
	 * 收藏菜品
	 * @return
	 */
	public String saveMenu(){
		if(this.getIds()!=null && !this.getIds().trim().equals("")){
			UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
			String[] idd = this.getIds().split(",");
			for(int i=0;i<idd.length;i++){
				ResMenu r = this.getResMenuService().findResMenuById(new Long(idd[i]));
				if(r!=null){
					Collect c = this.getCollectService().findCollectByUserIdObjIdObjType(us.getUid(), new Long(idd[i]), 1);
					if(c == null){
						c = new Collect();
						c.setUserId(us.getUid());
						c.setObjId(new Long(idd[i]));
						c.setObjType(1);
						c.setAddTime(new Date());
						c = this.getCollectService().saveCollect(c);
					}
				}
			}
			this.setList(this.getCollectService().findCollectByUserIdObjTypeFirstResultMaxResults(us.getUid(),1, "id", 1, 0, 5));
		}
		return "collectAjax";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String remove(){
		if(this.getId()>0){
			this.getCollectService().removeCollect(this.getId());
		}
		this.setAction("index");
		return "indexAction";
	}
	
	/**
	 * 通过ajax删除
	 * @return
	 */
	public String removeAjax(){
		if(this.getObjId()>0){
			UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
			Collect c = this.getCollectService().findCollectByUserIdObjIdObjType(us.getUid(), this.getObjId(), this.getObjType());
			if(c!=null){
				this.getCollectService().removeCollect(c);
			}
		}
		this.setResult("0");
		return "collectAjax";
	}
	
	/**
	 * 列表页收藏店铺
	 * @return
	 */
	public String saveRes2(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		if(this.getObjId()>0){
			//result 1 已存在 0 成功 2 失败
			Collect c = this.getCollectService().findCollectByUserIdObjIdObjType(us.getUid(), this.getObjId(), this.getObjType());
			if(c==null){
				c = new Collect();
				c.setUserId(us.getUid());
				c.setObjId(this.getObjId());
				c.setObjType(this.getObjType());
				c.setAddTime(new Date());
				c = this.getCollectService().saveCollect(c);
			}
		}
		this.setCollectList(this.getCollectService().findCollectByUserIdObjType(us.getUid(), this.getObjType()));
		return "collectAjax2";
	}
	
	/**
	 * 列表页通过ajax删除
	 * @return
	 */
	public String removeAjax2(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		if(this.getObjId()>0){
			Collect c = this.getCollectService().findCollectByUserIdObjIdObjType(us.getUid(), this.getObjId(), this.getObjType());
			if(c!=null){
				this.getCollectService().removeCollect(c);
			}
		}
		this.setCollectList(this.getCollectService().findCollectByUserIdObjType(us.getUid(), this.getObjType()));
		return "collectAjax2";
	}
}
